import Vue from "vue";
import VueRouter from "vue-router";
import Index from "@/views/admin/Index.vue";

Vue.use(VueRouter);

const routes = [
    {
        path: "/login",
        component: () => import("@/views/Login.vue"),
    },
    {
        path: "/",
        component: Index,
        children: [
            {
                path: "dashboard",
                component: () => import("@/views/admin/Dashboard.vue"),
            },
            {
                path: "users",
                component: () => import("@/views/admin/Users.vue"),
            },
            {
                path: "roles",
                component: () => import("@/views/admin/Roles.vue"),
            },
        ],
    },
];

const router = new VueRouter({
    mode: "history",
    base: process.env.BASE_URL,
    routes,
});

router.beforeEach((to, from, next) => {
    if (to.path === "/login" || window.sessionStorage.getItem("token")) {
        next();
    } else {
        next("/login");
    }
});

export default router;
